package com.leeall.utils.string;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * This is a String Util class for processing string 
 * @author Leeall (lixiaosong@gmail.com)
 */
public class StringUtil {

    private static final String HTTP_PREFIX = "http://";

    /**
     * Verify a String If it is null or empty.
     * @param s input string
     * @return  whether the string is empty
     */
    public static boolean stringIsEmpty(String s) {
        if (s == null || s.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Verify a String If it is null or empty.
     * @param s input string
     * @return  whether the string is empty
     */
    public static boolean stringIsNotEmpty(String s) {
        return !stringIsEmpty(s);
    }

    /**
     * Validate Email Address Format
     * @param email
     * @return
     */
    public static boolean validateEmail(String email) {
        if (stringIsNotEmpty(email) && email.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+")) {
            return true;
        }
        return false;
    }

    /**
     * Token a string
     * @param original  original string
     * @param regex     regular expression
     * @param returnDelims  
     * @return  a string List
     */
    public static List<String> tokenString(String original, String regex,
            boolean returnDelims) {
        StringTokenizer token = new StringTokenizer(original, regex,
                returnDelims);
        List<String> list = new ArrayList<String>();
        while (token.hasMoreTokens()) {
            list.add(token.nextToken().trim());
        }
        return list;
    }

    /**
     * Insert a HTML URL markup from a string which contains url information
     * @param in    input string
     * @return  output string that including HTML markup
     */
    public static String insertLink(String in) {
        String tmp = in;
        StringBuilder sb = new StringBuilder();
        while (tmp.indexOf(HTTP_PREFIX) != -1) {
            if (tmp.indexOf(HTTP_PREFIX) > 0) {
                sb.append(tmp.substring(0, tmp.indexOf(HTTP_PREFIX)));
                tmp = tmp.substring(tmp.indexOf(HTTP_PREFIX));
            }
            for (int i = 0; i < tmp.length(); i++) {
                if (i == (tmp.length() - 1)) {
                    sb.append("<a href=\"" + tmp + "\" target=\"_blank\">" + tmp + "</a>");
                    tmp = "";
                    break;
                }
                Character c = tmp.charAt(i);

                if (Character.isSpaceChar(c)) {
                    String t1 = tmp.substring(0, i);
                    sb.append("<a href=\"" + t1 + "\" target=\"_blank\">" + t1 + "</a>");
                    tmp = tmp.substring(i);
                    break;
                } else if (c > 256) {
                    String t1 = tmp.substring(0, i);
                    sb.append("<a href=\"" + t1 + "\" target=\"_blank\">" + t1 + "</a>");
                    tmp = tmp.substring(i);
                    break;
                }
            }
        }
        sb.append(tmp);
        return sb.toString();
    }

    public static void main(String[] args) {

    }
}
